package com.tianruan.dc.modules.exam.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import com.tianruan.dc.common.api.ApiRest;
import com.tianruan.dc.common.api.controller.BaseController;
import com.tianruan.dc.common.api.dto.BaseIdReqDTO;
import com.tianruan.dc.common.api.dto.BaseIdsReqDTO;
import com.tianruan.dc.common.api.dto.BaseStateReqDTO;
import com.tianruan.dc.common.api.dto.PagingReqDTO;
import com.tianruan.dc.common.utils.response.VResponse;
import com.tianruan.dc.modules.exam.dto.ExamDTO;
import com.tianruan.dc.modules.exam.dto.request.ExamSaveReqDTO;
import com.tianruan.dc.modules.exam.dto.response.ExamOnlineRespDTO;
import com.tianruan.dc.modules.exam.dto.response.ExamReviewRespDTO;
import com.tianruan.dc.modules.exam.entity.Exam;
import com.tianruan.dc.modules.exam.service.ExamService;
import com.tianruan.dc.modules.qu.dto.QuDTO;
import com.tianruan.dc.modules.sys.user.entity.WorkInfo;
import com.tianruan.dc.modules.sys.user.utils.PageBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
* <p>
* 考试控制器
* </p>
*/
@Api(tags={"考试"})
@RestController
@RequestMapping("/api/exam")
public class ExamController extends BaseController {

    @Autowired
    private ExamService baseService;

    /**
    * 添加或修改
    * @param reqDTO
    * @return
    */

    @ApiOperation(value = "添加或修改")
    @RequestMapping(value = "/save", method = { RequestMethod.POST})
    public ApiRest save(@RequestBody ExamSaveReqDTO reqDTO) {
        //复制参数
        baseService.save(reqDTO);
        return super.success();
    }

    /**
    * 批量删除
    * @param reqDTO
    * @return
    */
    @ApiOperation(value = "批量删除")
    @RequestMapping(value = "/delete", method = { RequestMethod.POST})
    public ApiRest edit(@RequestBody BaseIdsReqDTO reqDTO) {
        //根据ID删除
        baseService.removeByIds(reqDTO.getIds());
        return super.success();
    }

    /**
    * 查找详情
    * @param reqDTO
    * @return
    */
    @ApiOperation(value = "查找详情")
    @RequestMapping(value = "/detail", method = { RequestMethod.POST})
    public ApiRest<ExamSaveReqDTO> find(@RequestBody BaseIdReqDTO reqDTO) {
        ExamSaveReqDTO dto = baseService.findDetail(reqDTO.getId());
        return super.success(dto);
    }

    /**
     * 查找详情
     * @param reqDTO
     * @return
     */

    @ApiOperation(value = "查找详情")
    @RequestMapping(value = "/state", method = { RequestMethod.POST})
    public ApiRest state(@RequestBody BaseStateReqDTO reqDTO) {

        QueryWrapper<Exam> wrapper = new QueryWrapper<>();
        wrapper.lambda().in(Exam::getId, reqDTO.getIds());
        Exam exam = new Exam();
        exam.setState(reqDTO.getState());
        exam.setUpdateTime(new Date());

        baseService.update(exam, wrapper);
        return super.success();
    }


    /**
     * 分页查找
     * @param
     * @return
     */
//    @ApiOperation(value = "考试视角")
//    @RequestMapping(value = "/online-paging", method = { RequestMethod.POST})
//    public ApiRest<IPage<ExamOnlineRespDTO>> myPaging(@RequestBody PagingReqDTO<ExamDTO> reqDTO) {
//
//        //分页查询并转换
//        IPage<ExamOnlineRespDTO> page = baseService.onlinePaging(reqDTO);
//        return super.success(page);
//    }

    /*
    个人成绩分页查询
     */
    @PostMapping("/my-paging")
    //授权
    @PreAuthorize("hasAnyAuthority('sys:super:admin','sys:teacher','sys:student')")
    public VResponse<PageBean<Exam>> pageWorkInfo(
            @RequestParam(defaultValue = "1")  Integer page,
            @RequestParam(defaultValue = "10")  Integer pageSize,
            @RequestParam(value = "userId",required = false) String userId
    ){
        PageBean<Exam> exam=baseService.PageWorkInfo(page,pageSize,userId);
        return VResponse.success(exam);
    }

    /**
    * 分页查找
    * @param reqDTO
    * @return
    */
    @ApiOperation(value = "分页查找")
    @RequestMapping(value = "/paging", method = { RequestMethod.POST})
    public ApiRest<IPage<ExamDTO>> paging(@RequestBody PagingReqDTO<ExamDTO> reqDTO) {

        //分页查询并转换
        IPage<ExamDTO> page = baseService.paging(reqDTO);

        return super.success(page);
    }
    @PostMapping("/paging-exam")
    //授权
    @PreAuthorize("hasAnyAuthority('sys:super:admin','sys:teacher','sys:student')")
    public VResponse<PageBean<ExamDTO>> pageInfo(
            @RequestParam(defaultValue = "1")  Integer page,
            @RequestParam(defaultValue = "10")  Integer pageSize
    ){
        PageBean<ExamDTO> qu=baseService.PageExam(page,pageSize);
        return VResponse.success(qu);
    }


    /**
     * 分页查找
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "待阅试卷")
    @RequestMapping(value = "/review-paging", method = { RequestMethod.POST})
    public ApiRest<IPage<ExamReviewRespDTO>> reviewPaging(@RequestBody PagingReqDTO<ExamDTO> reqDTO) {
        //分页查询并转换
        IPage<ExamReviewRespDTO> page = baseService.reviewPaging(reqDTO);
        return super.success(page);
    }


}
